package com.jinzhi.jzweb.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.CertificateNewsDO;
import com.jinzhi.jzweb.domain.CertificateProfileDO;
import com.jinzhi.jzweb.service.CertificateNewsService;
import com.jinzhi.jzweb.service.CertificateProfileService;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <pre>
 * 证书信息
 * </pre>
 * <small> 2020-02-24 21:00:12 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/certificateNews")
public class CertificateNewsController extends BaseController {
    @Autowired
    private CertificateNewsService certificateNewsService;
    @Autowired
    private UserService userService;
    @Autowired
    private CertificateProfileService certificateProfileService;

    @GetMapping()
    @RequiresPermissions("jzweb:certificateNews:certificateNews")
    String CertificateNews() {
        return "jzweb/certificateNews/certificateNews";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("jzweb:certificateNews:certificateNews")
    public Result<Page<CertificateNewsDO>> list(CertificateNewsDO certificateNewsDTO) {
        Wrapper<CertificateNewsDO> wrapper = new EntityWrapper<CertificateNewsDO>().orderBy("id", false);
        if (certificateNewsDTO.getState() != null){
            wrapper.eq("state",certificateNewsDTO.getState());
        }
        if (certificateNewsDTO.getGrade() != null){
            wrapper.eq("grade",certificateNewsDTO.getGrade());
        }
        if (StringUtils.isNotBlank(certificateNewsDTO.getProfileName())){
            Wrapper<CertificateProfileDO> cerWrapper = new EntityWrapper<CertificateProfileDO>().like("name",certificateNewsDTO.getProfileName());
            List<CertificateProfileDO>  certificateProfileDOS = certificateProfileService.selectList(cerWrapper);
            String str = "";
            for (CertificateProfileDO certificateProfileDO : certificateProfileDOS){
                str = str + certificateProfileDO.getId() + ",";
            }
            wrapper.and();
            if(StringUtils.isNotBlank(str)){
                wrapper.in("profile_id",str.substring(0,str.length()-1));
            }else{
                wrapper.eq("profile_id",null);
            }
        }

        Page<CertificateNewsDO> page = certificateNewsService.selectPage(getPage(CertificateNewsDO.class), wrapper);
        List list = new ArrayList();
        for (int i = 0; i < page.getRecords().size(); i++) {
            CertificateNewsDO obj = (CertificateNewsDO) page.getRecords().get(i);
            //创建用户名
            if (obj.getCreaterId() != null) {
                UserDO userDO = userService.selectById(obj.getCreaterId());
                if (userDO != null) {
                    obj.setCreaterName(userDO.getUsername());
                }
            }
            //审核用户名
            if (obj.getCheckerId() != null) {
                UserDO userDO = userService.selectById(obj.getCheckerId());
                if (userDO != null) {
                    obj.setCheckerName(userDO.getUsername());
                }
            }
            //证书名称
            if (obj.getProfileId() != null) {
                CertificateProfileDO certificateProfileDO = certificateProfileService.selectById(obj.getProfileId());
                if (certificateProfileDO != null) {
                    obj.setProfileName(certificateProfileDO.getName());
                }
            }
            list.add(obj);
        }
        page.setRecords(list);
        return Result.ok(page);
    }

    @GetMapping("/add")
    @RequiresPermissions("jzweb:certificateNews:add")
    String add(Model model) {
        model.addAttribute("certificateProfile", certificateProfileService.selByAll());
        return "jzweb/certificateNews/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("jzweb:certificateNews:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        CertificateNewsDO certificateNews = certificateNewsService.selectById(id);
        model.addAttribute("certificateProfile", certificateProfileService.selByAll());
        model.addAttribute("certificateNews", certificateNews);
        return "jzweb/certificateNews/edit";
    }

    @GetMapping("/info/{id}")
    @RequiresPermissions("jzweb:certificateNews:certificateNews")
    String info(@PathVariable("id") Long id, Model model) {
        CertificateNewsDO certificateNews = certificateNewsService.selectById(id);
        //创建用户名
        if (certificateNews.getCreaterId() != null) {
            UserDO userDO = userService.selectById(certificateNews.getCreaterId());
            if (userDO != null) {
                certificateNews.setCreaterName(userDO.getUsername());
            }
        }
        //审核用户名
        if (certificateNews.getCheckerId() != null) {
            UserDO userDO = userService.selectById(certificateNews.getCheckerId());
            if (userDO != null) {
                certificateNews.setCheckerName(userDO.getUsername());
            }
        }
        //证书名称
        if (certificateNews.getProfileId() != null) {
            CertificateProfileDO certificateProfileDO = certificateProfileService.selectById(certificateNews.getProfileId());
            if (certificateProfileDO != null) {
                certificateNews.setProfileName(certificateProfileDO.getName());
            }
        }
        model.addAttribute("certificateNews", certificateNews);
        return "jzweb/certificateNews/info";
    }

    @Log("添加证书信息")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("jzweb:certificateNews:add")
    public Result<String> save(CertificateNewsDO certificateNews) {
        certificateNews.setCreaterId(this.getUserId());
        certificateNewsService.insert(certificateNews);
        return Result.ok();
    }

    @Log("修改证书信息")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("jzweb:certificateNews:edit")
    public Result<String> update(CertificateNewsDO certificateNews) {
        boolean update = certificateNewsService.updateById(certificateNews);
        return update ? Result.ok() : Result.fail();
    }

    @Log("删除证书信息")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("jzweb:certificateNews:remove")
    public Result<String> remove(Long id) {
        certificateNewsService.deleteById(id);
        return Result.ok();
    }

    @Log("批量删除证书信息")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("jzweb:certificateNews:batchRemove")
    public Result<String> remove(@RequestParam("ids[]") Long[] ids) {
        certificateNewsService.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }

    @Log("审核")
    @PostMapping("/review")
    @ResponseBody
    @RequiresPermissions("jzweb:certificateNews:review")
    public Result<String> review(@RequestParam("ids[]") Long[] ids, Integer state, String remark) {
        if (state.equals(2) || state.equals(1)) {
            int i = 0;
            for (Long id : ids) {
                CertificateNewsDO certificateNewsDO = certificateNewsService.selectById(id);
                if (certificateNewsDO != null) {
                    certificateNewsDO.setState(state);
                    certificateNewsDO.setCheckerId(this.getUserId());
                    certificateNewsDO.setCheckerDate(new Date());
                    if (state.equals(2)) {
                        certificateNewsDO.setRemark(remark);//未通过，添加备注
                    } else {
                        certificateNewsDO.setRemark("");//审核通过，清空备注
                    }
                    boolean b = certificateNewsService.updateById(certificateNewsDO);
                    if (b) {
                        i++;
                    } else {
                        break;
                    }
                }
            }
            if (i == ids.length) {
                return Result.build(0, "已成功审核：" + i + "条数据");
            } else {
                return Result.fail("审核状态修改失败！");
            }
        } else {
            return Result.fail("审核状态修改失败！");
        }
    }

}
